function extensionLocation( extension_id, item ) // Extension ID is located in install.rdf
{
	// the extension's id from install.rdf
	var em = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager);

	// returns nsIFile for my extension's platform forlder
	return em.getInstallLocation(extension_id).getItemFile(extension_id, item);

} // End of function

function checkOS() 
{
	if(navigator.userAgent.indexOf('Linux') != -1)
		{ var OpSys = "Linux"; }
	else if((navigator.userAgent.indexOf('Win') != -1))
		{ var OpSys = "Windows"; } 
	else if(navigator.userAgent.indexOf('Mac') != -1)
		{ var OpSys = "Macintosh"; }
	else { var OpSys = "operating system not recognised"; }

	return OpSys;
} // End of Function

function readFile( nsIFile )
{
	// open an input stream from file
	var istream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
	istream.init(nsIFile, 0x01, 0444, 0);
	istream.QueryInterface(Components.interfaces.nsILineInputStream);

	// read lines into array
	var line = {}, lines = [], hasmore;
	do {
	  hasmore = istream.readLine(line);
	  lines.push(line.value); 
	} while(hasmore);

	istream.close();
	
	return lines;
} // End of Function

function download()
{
	if ( username )
	{
				var system = checkOS();
				var currentProfile = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsILocalFile);
				var profileFolder = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("DefProfRt", Components.interfaces.nsILocalFile);
				
				var settings = readFile( username );
					var user_name = settings[1];
					var password = report( 'password', 'value' );
				
				var extension = extensionLocation( "syncfox@ahir.reddy", 'platform' );
					var pZip = extension.clone();
					var download = extension.clone();
						download.append('imap');
						
				var python = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);

// Operating System Depended Operations
				if ( system == 'Windows' )
				{
					pZip.append('7za.exe');
					var pzip_args = ["x", profileFolder.path + "\\" + currentProfile.leafName + ".profile.7z", "-o" + currentProfile.path, "-y" ]; 
					
					download.append('download.exe');
					var download_args = [user_name, password, profileFolder.path + "\\" + currentProfile.leafName + ".profile.7z" ];
					
					var force_del = extension.clone();
						force_del.append('ForceDel.exe');
						var exec = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
						exec.init(force_del);
					
					var file_list = "places.sqlite cookies.sqlite formhistory.sqlite key3.db signons3.txt permissions.sqlite user.js prefs.js webappsstore.sqlite persdict.dat content-prefs.sqlite chrome extensions Chrome Extensions searchplugins";
						var temp_arg = new Array();
						file_list = file_list.split(' ');
						while ( file_list.length != 0 )
						{
							var temp = file_list.pop();
							temp = currentProfile.path + "\\" + temp;
							temp_arg.push(temp);
							exec.run(true, temp_arg, temp_arg.length);
							temp_arg.pop();
						}
				}
				
				else if ( system == 'Linux' )
				{
					pZip.append('7zaLinux');
					var pzip_args = ["x", profileFolder.path + "/" + currentProfile.leafName + ".profile.7z", "-o" + currentProfile.path, "-y" ]; 
					
					python.initWithPath('/usr/bin/python');
					
					download.append('download.py');
					var download_args = [download.path, user_name, password, profileFolder.path + "/" + currentProfile.leafName + ".profile.7z" ];
					download = python;
				}
							
				else if ( system == 'Macintosh' )
				{
					pZip.append('7zaMac');
					var pzip_args = ["x", profileFolder.path + "/" + currentProfile.leafName + ".profile.7z", "-o" + currentProfile.path, "-y" ]; 
					
					python.initWithPath('/usr/bin/python');
					
					download.append('download.py');
					var download_args = [download.path, user_name, password, profileFolder.path + "/" + currentProfile.leafName + ".profile.7z" ];
					download = python;
				}
	
// External Process Execution								
				var exec = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
				
				exec.init(download);
				exec.run(true, download_args, download_args.length);
				alert("Profile Downloaded");

				exec.init(pZip);
				alert("Decompressing...");
				exec.run(true, pzip_args, pzip_args.length);
				restartApp();
	
	} // End of major if statement
	
	else
	{
				window.open("chrome://syncfox/content/setup.xul", "Syncfox Setup", "chrome");
				self.close();
	}
	
} // End of Function

function sync()
{
	if ( username )
	{
				var system = checkOS();
				var currentProfile = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsILocalFile);
				var profileFolder = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("DefProfRt", Components.interfaces.nsILocalFile);
				
				var settings = readFile( username );
					var user_name = settings[1];
					var password = report( 'password', 'value' );
				
				var extension = extensionLocation( "syncfox@ahir.reddy", 'platform' );
					var pZip = extension.clone();
					
					var imap = extension.clone();
						imap.append('imap');
							template = imap.clone();			
							template.append('template.eml');
						
				var python = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);

				if ( system == 'Windows' )
					{	var	separator = "\\"; }
				else
					{ var separator = '/'; }

// Operating System Depended Operations
				if ( system == 'Windows' )
				{
					pZip.append('7za.exe');
					var pzip_args = ["u", "-r", "-ssw", profileFolder.path + "\\" + currentProfile.leafName + ".profile.7z"]; 
					
					imap.append('imap.exe');
					var imap_args = [profileFolder.path + "\\" + currentProfile.leafName + ".profile.7z", user_name, password, template.path ];
				}
				
				else if ( system == 'Linux' )
				{
					pZip.append('7zaLinux');
					var pzip_args = ["u", profileFolder.path + "/" + currentProfile.leafName + ".profile.7z"]; 
					
					python.initWithPath('/usr/bin/python');
					
					imap.append('imap.py');
					var imap_args = [imap.path, profileFolder.path + "/" + currentProfile.leafName + ".profile.7z", user_name, password, template.path ];
					imap = python;
				}
							
				else if ( system == 'Macintosh' )
				{
					pZip.append('7zaMac');
					var pzip_args = ["u", profileFolder.path + "/" + currentProfile.leafName + ".profile.7z"]; 
					
					python.initWithPath('/usr/bin/python');
					
					imap.append('imap.py');
					var imap_args = [imap.path, profileFolder.path + "/" + currentProfile.leafName + ".profile.7z", user_name, password, template.path ];
					imap = python;
				}

				// List of files to compress in Profile Folder
				// pushes them as individual args in pzip_args
				var file_list = "places.sqlite cookies.sqlite formhistory.sqlite key3.db signons3.txt permissions.sqlite user.js prefs.js webappsstore.sqlite persdict.dat content-prefs.sqlite chrome extensions Chrome Extensions searchplugins";
					file_list = file_list.split(' ');
					while ( file_list.length != 0 )
					{
						var temp = file_list.pop();
						temp = currentProfile.path + separator + temp;
						pzip_args.push(temp);
					}
	
// External Process Execution								
				var exec = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);

				exec.init(pZip);
				alert("Press OK to Compress");
				exec.run(true, pzip_args, pzip_args.length);
				alert("Done, press OK to Upload");
				
				exec.init(imap);
				exec.run(false, imap_args, imap_args.length);

	} // End of major if statement
	
	else
	{
				window.open("chrome://syncfox/content/setup.xul", "Syncfox Setup", "chrome");
				self.close();
	}
	
} // End of Function
